import 'package:qr_code_app/common/api/api_convert.dart';
import 'package:qr_code_app/common/api/api_interceptor.dart';
import 'package:qr_code_app/common/api/api_service.dart';
import 'package:todo_flutter/todo_flutter.dart';

class ApiProvider extends BaseNetProvider {
  factory ApiProvider() => _getInstance();

  static ApiProvider get instance => _getInstance();
  static ApiProvider? _instance;

  static ApiProvider _getInstance() {
    _instance ??= ApiProvider._internal();
    return _instance!;
  }

  DioEngine dioEngine = DioEngine(ApiService.currentByEnv.url);
  ApiConvert apiConvert = ApiConvert();

  ApiProvider._internal() {
    final apiInfo = ApiService.currentByEnv;
    if (apiInfo.proxy) {
      dioEngine.setProxy(apiInfo.proxyIp, apiInfo.proxyPort);
    }
    dioEngine.addInterceptor(ApiInterceptor());
  }

  @override
  BaseNetEngine get engine => dioEngine;

  @override
  BaseConvert get convert => apiConvert;
}

abstract class ApiRequest<T> extends BaseRequest<T> {
  ApiRequest(super.params);

  @override
  String get pageKey => 'page';

  @override
  String get pageSizeKey => 'page_size';

  @override
  BaseNetProvider get netProvider => ApiProvider.instance;
}
